package com.mate.cloud.auth.domain;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SpringSecurity需要的用户详情
 * @author:         MI
 * @email:          448341911@qq.com
 * @createTime:     2024/6/14 22:33
 * @updateUser:     MI
 * @updateTime:     2024/6/14 22:33
 * @updateRemark:   修改内容
 * @version:        v1.0
 */
public class MateUserDetails implements UserDetails {

    private Integer id;

    private UmsAdmin umsAdmin;


    private List<UmsResource> resourceList;

    public MateUserDetails(Integer id, UmsAdmin umsAdmin, List<UmsResource> resourceList) {
        this.id = id;
        this.umsAdmin = umsAdmin;
        this.resourceList = resourceList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的角色
        return resourceList.stream()
                .map(role -> new SimpleGrantedAuthority(role.getId() + ":" + role.getName()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return umsAdmin.getPassword();
    }

    @Override
    public String getUsername() {
        return umsAdmin.getUsername();
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return umsAdmin.getStatus().equals(1);
    }

    public Integer getId() {
        return umsAdmin.getId();
    }
}
